package com.fishery.refrigeratory.controller;


import com.fishery.entity.Result;
import com.fishery.refrigeratory.entity.Refrigeratory;
import com.fishery.refrigeratory.entity.vo.ProductVo;
import com.fishery.refrigeratory.entity.vo.RefrigeratoryInfoVo;
import com.fishery.refrigeratory.service.RefrigeratoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wgz
 * @since 2021/1/20
 */


@Api(description = "冷库信息接口")
@RestController
@CrossOrigin
@RequestMapping("/refrigeratory/RefrigeratoryInfo")
public class RefrigeratoryController {

    @Autowired
    private RefrigeratoryService refrigeratoryService;

    /**
     * 创建冷库
     * @param refrigeratory
     * @return
     */
    @PostMapping("/in")
    @ApiOperation("创建冷库")
//    @ApiImplicitParam(paramType = "body", name = "refrigeratory", value = "{\n" +
//            "\t\t\t\t\"refrigeratoryName\": \"冷库名称\",\n" +
//            "\t\t\t\t\"refrigeratoryAddress\": \"冷库地址\",\n" +
//            "\t\t\t\t\"refrigeratoryArea\": 123,\n" +
//            "\t\t\t\t\"refrigeratoryPositionLongitude\": \"冷库地址经度\",\n" +
//            "\t\t\t\t\"refrigeratoryPositionLatitude\": \"冷库地址纬度\",\n" +
//            "\t\t\t\t\"refrigeratoryUsedCapacity\": 0,\n" +
//            "\t\t\t\t\"refrigeratoryCapacity\": 1000,\n" +
//            "\t\t\t\t\"refrigeratoryTemperature\": 10, \n" +
//            "\t\t\t\t\"refrigeratoryIntroduce\": \"冷库介绍\",\n" +
//            "\t\t\t\t\"createPersonId\": \"1352634961499963394\",\n" +
//            "\t\t\t\t\"baseId\": \"1350657222372835330\"\n" +
//            "\t}", required = true, dataType = "Refrigeratory")
    public Result createRefrigeratory (@RequestBody @ApiParam("冷库信息") Refrigeratory refrigeratory) {
        if (refrigeratory == null
                || StringUtils.isEmpty(refrigeratory.getRefrigeratoryName())
                || StringUtils.isEmpty(refrigeratory.getRefrigeratoryAddress())
                || StringUtils.isEmpty(refrigeratory.getRefrigeratoryPositionLatitude())
                || StringUtils.isEmpty(refrigeratory.getRefrigeratoryPositionLongitude())
                || StringUtils.isEmpty(refrigeratory.getCreatePersonId())
                || StringUtils.isEmpty(refrigeratory.getBaseId())
                ) {
            return Result.error("数据不合法！");
        } else if (StringUtils.isEmpty(refrigeratory.getRefrigeratoryCapacity()) || refrigeratory.getRefrigeratoryCapacity() <= 0) {
            return Result.error("请输入合法的冷库总容量");
        } else if (StringUtils.isEmpty(refrigeratory.getRefrigeratoryUsedCapacity()) || refrigeratory.getRefrigeratoryCapacity() < refrigeratory.getRefrigeratoryUsedCapacity()){
            return Result.error("冷库已用容量不能超过总容量");
        } else if (StringUtils.isEmpty(refrigeratory.getRefrigeratoryArea()) || refrigeratory.getRefrigeratoryArea() <= 0) {
            return Result.error("请输入合法的面积");
        }
        return refrigeratoryService.createRefrigeratory(refrigeratory)? Result.success("创建成功"): Result.error("创建失败");
    }

    /**
     * 修改冷库
     * @param refrigeratory
     * @return
     */
    @PutMapping("/in")
    @ApiOperation("修改冷库信息")
//    @ApiImplicitParam(paramType = "body", name = "refrigeratory", value = "{\n" +
//            "\t\t\t\t\"id\": \"1366643839640641537\",\n" +
//            "\t\t\t\t\"refrigeratoryName\": \"修改冷库名称\",\n" +
//            "\t\t\t\t\"refrigeratoryAddress\": \"修改冷库地址\",\n" +
//            "\t\t\t\t\"refrigeratoryArea\": 123,\n" +
//            "\t\t\t\t\"refrigeratoryPositionLongitude\": \"修改冷库地址经度\",\n" +
//            "\t\t\t\t\"refrigeratoryPositionLatitude\": \"修改冷库地址纬度\",\n" +
//            "\t\t\t\t\"refrigeratoryUsedCapacity\": 100,\n" +
//            "\t\t\t\t\"refrigeratoryCapacity\": 1000,\n" +
//            "\t\t\t\t\"refrigeratoryTemperature\": 0, \n" +
//            "\t\t\t\t\"refrigeratoryIntroduce\": \"修改冷库介绍\",\n" +
//            "\t\t\t\t\"createPersonId\": \"1352634961499963394\"\n" +
//            "\t}", required = true, dataType = "Refrigeratory")
    public Result updateRefrigeratory (@RequestBody @ApiParam("修改的冷库信息") Refrigeratory refrigeratory) {
        System.out.println(refrigeratory);
        if (refrigeratory == null
            || StringUtils.isEmpty(refrigeratory.getRefrigeratoryName())
            || StringUtils.isEmpty(refrigeratory.getRefrigeratoryAddress())
            || StringUtils.isEmpty(refrigeratory.getRefrigeratoryPositionLongitude())
            || StringUtils.isEmpty(refrigeratory.getRefrigeratoryPositionLatitude())
            || StringUtils.isEmpty(refrigeratory.getBaseId())
            || StringUtils.isEmpty(refrigeratory.getCreatePersonId())
            || StringUtils.isEmpty(refrigeratory.getRefrigeratoryArea())
        ) {
            return Result.error("数据为空！请重新输入！");
        } else if (StringUtils.isEmpty(refrigeratory.getRefrigeratoryCapacity()) || refrigeratory.getRefrigeratoryCapacity() <= 0) {
            return Result.error("请输入合法的冷库总容量");
        } else if (StringUtils.isEmpty(refrigeratory.getRefrigeratoryUsedCapacity()) || refrigeratory.getRefrigeratoryCapacity() < refrigeratory.getRefrigeratoryUsedCapacity()){
            return Result.error("冷库已用容量不能超过总容量");
        }
        return refrigeratoryService.updateRefrigeratory(refrigeratory)? Result.success("修改成功"): Result.error("修改失败");
    }

    /**
     * 删除冷库
     * @param id
     * @return
     */
    @DeleteMapping("/in/{id}")
    @ApiOperation("删除冷库")
    public Result deleteRefrigeratory(@PathVariable(value = "id") @ApiParam("冷库id") String id) {
        return refrigeratoryService.deleteRefrigeratory(id)? Result.success("删除成功"): Result.error("删除失败");
    }

    /**
     * 查询所有的冷库信息
     * @return
     */
    @GetMapping("/get/AllRefrigeratory")
    @ApiOperation("查询所有的冷库信息")
    public Result getAllRefrigeratory() {
        return Result.success().data(refrigeratoryService.getAllRefrigeratory());
    }

    /**
     * 分页查询所有的冷库信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/get/AllRefrigeratory/{page}/{size}")
    @ApiOperation("分页查询所有的冷库信息")
    public Result AllPageRefrigeratory(@PathVariable(value = "page") @ApiParam("页数") Integer page,
                                       @PathVariable(value = "size") @ApiParam("行数") Integer size) {
        page = page <= 0 ? 1 : page;
        size = size <= 0 ? 1: size;
        return Result.success().data(refrigeratoryService.getAllPageRefrigeratory(page,size));
    }


    /**
     * 根据冷库id查询冷库
     * @param id
     * @return
     */
    @GetMapping("/get/Refrigeratory/{id}")
    @ApiOperation("根据冷库id查询冷库")
    public Result getRefrigeratory(@PathVariable(value = "id") @ApiParam("冷库id") String id) {
        Refrigeratory refrigeratoryList = refrigeratoryService.getRefrigeratory(id);
        if (refrigeratoryList == null) {
            return Result.error("没有查到该冷库");
        }
        return Result.success("查询成功").data(refrigeratoryList);
    }

    /**
     * 根据基地id查询冷库
     * @param baseId
     * @return
     */
    @GetMapping("get/RefrigeratoryByBaseId/{baseId}")
    @ApiOperation("根据基地id查询冷库")
    public Result searchFactoriesOfBase(@PathVariable(value = "baseId") @ApiParam("基地id") String baseId){
        List<Refrigeratory> refrigeratoryList = refrigeratoryService.getRefrigeratoryByBaseId(baseId);
        if (refrigeratoryList.size() > 0) {
            return Result.success("查询成功").data(refrigeratoryList);
        }
        return Result.error("没有查到该冷库");
    }


    /**
     * 根据基地id分页查询冷库
     * @param base_id
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/get/PageRefrigeratoryByBaseId/{baseId}/{page}/{size}")
    @ApiOperation("根据基地id分页查询冷库")
    public Result PageRefrigeratoryByBaseId(@PathVariable(value = "baseId") String base_id,
                                                                  @PathVariable(value = "page") Integer page,
                                                                  @PathVariable(value = "size") Integer size) {
        page = page <= 0 ? 1 : page;
        size = size <= 0 ? 1: size;
        return Result.success().data(refrigeratoryService.PageRefrigeratoryByBaseId(base_id,page,size));
    }

    /**
     * 通过基地id获取所有冷库的面积
     * @return
     */
    @ApiOperation(value = "通过基地id获取所有冷库的面积")
    @GetMapping("/getAllArea/{id}")
    public Result selectAllArea(@PathVariable(value = "id", required = true) @ApiParam(value = "基地id", required = true) String baseId) {
        return Result.success("查询成功").data(refrigeratoryService.selectAllAreaByBaseId(baseId));
    }

    /**
     * 通过基地id获取所有冷库的数量
     * @param baseId
     * @return
     */
    @ApiOperation(value = "通过基地id获取所有冷库的数量")
    @GetMapping("/getAllCount/{id}")
    public Result selectAllRefrigeratoryCount(@PathVariable(value = "id", required = true) @ApiParam(value = "基地id", required = true) String baseId) {
        return Result.success("查询成功").data(refrigeratoryService.selectAllRefrigeratoryCount(baseId));
    }

    /**
     * 查询所有的冷库收货地址信息（是不是要排除当前的冷库，还是当前的基地）
     * @return
     */
    @GetMapping("/get/AllRefrigeratoryInfo")
    @ApiOperation("查询所有的冷库收货地址信息")
    public Result AllRefrigeratoryInfo() {
        return Result.success().data(refrigeratoryService.getAllRefrigeratory());
    }

}
